package com.parking.status.sync.netty.service;


import com.parking.status.sync.entity.*;
import com.parking.status.sync.netty.entity.*;
import com.parking.status.sync.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by Administrator on 2015/9/25.
 */
@Component
public class CommandFactory {

    @Autowired
    CommandSyncParkingInfoRepository commandSyncParkingInfoRepository;
    @Autowired
    CommandParkingInRepository commandParkingInRepository;
    @Autowired
    CommandParkingOutRepository commandParkingOutRepository;
    @Autowired
    CommandParkingCheckinRepository commandParkingCheckinRepository;
    @Autowired
    CommandHeartBeatRepository commandHeartBeatRepository;

    public CommandBase createCommand(RequestPre request) {
        CommandBase command = null;
        if (request instanceof RequestSyncParkingInfo) {
            RequestSyncParkingInfo req = (RequestSyncParkingInfo)request;
            command = new CommandSyncParkingInfo(
                    0,
                    req.getCommRequest().getClientId(),
                    req.getCommRequest().getTsn(),
                    req.getCommRequest().getSim(),
                    req.getCommRequest().getPsam(),
                    req.getCommRequest().getSysVer(),
                    req.getCommRequest().getAppVer(),
                    req.getUid(),
                    req.getBatchCode(),
                    req.getBizSn(),
                    req.getActTime(),
                    req.getParkingSpotId(),
                    req.getPlatformId(),
                    req.getTotBerthNum(),
                    req.getMonthlyBerthNum(),
                    req.getGuesBerthNum(),
                    req.getTotRemainNum(),
                    req.getMonthlyRemainNum(),
                    req.getGuestRemainNum()
            );
        } else if(request instanceof RequestParkingIn){
            RequestParkingIn req = (RequestParkingIn)request;
            command = new CommandParkingIn(
                    0,
                    req.getCommRequest().getClientId(),
                    req.getCommRequest().getTsn(),
                    req.getCommRequest().getSim(),
                    req.getCommRequest().getPsam(),
                    req.getCommRequest().getSysVer(),
                    req.getCommRequest().getAppVer(),
                    req.getUid(),
                    req.getBusinessLogList(),
                    req.getBatchCode(),
                    req.getBizSn(),
                    req.getParkingSpotId(),
                    req.getPlatformId(),
                    req.getBerthId(),
                    req.getAddBerth(),
                    req.getBusinessType(),
                    req.getActType(),
                    req.getActTime(),
                    req.getCarNumber(),
                    req.getMonthlyCertNumber(),
                    req.getCarType(),
                    req.getTotRemainNum(),
                    req.getMonthlyRemainNum(),
                    req.getGuestRemainNum(),
                    req.getVoucherType(),
                    req.getVoucherNo()
            );
        } else if(request instanceof RequestParkingOut){
            RequestParkingOut req = (RequestParkingOut)request;
            command = new CommandParkingOut(
                    0,
                    req.getCommRequest().getClientId(),
                    req.getCommRequest().getTsn(),
                    req.getCommRequest().getSim(),
                    req.getCommRequest().getPsam(),
                    req.getCommRequest().getSysVer(),
                    req.getCommRequest().getAppVer(),
                    req.getUid(),
                    req.getBusinessLogList(),
                    req.getBatchCode(),
                    req.getBizSn(),
                    req.getParkingSpotId(),
                    req.getPlatformId(),
                    req.getBerthId(),
                    req.getAddBerth(),
                    req.getBusinessType(),
                    req.getActType(),
                    req.getActTime(),
                    req.getCarNumber(),
                    req.getMonthlyCertNumber(),
                    req.getCarType(),
                    req.getTotRemainNum(),
                    req.getMonthlyRemainNum(),
                    req.getGuestRemainNum(),
                    req.getParkingTimeLength(),
                    req.getPayMoney(),
                    req.getPaymentType(),
                    req.getVoucherType(),
                    req.getVoucherNo()
            );
        } else if(request instanceof RequestParkingCheckin){
            RequestParkingCheckin req = (RequestParkingCheckin)request;
            command = new CommandParkingCheckin(
                    0,
                    req.getCommRequest().getClientId(),
                    req.getCommRequest().getTsn(),
                    req.getCommRequest().getSim(),
                    req.getCommRequest().getPsam(),
                    req.getCommRequest().getSysVer(),
                    req.getCommRequest().getAppVer(),
                    req.getParkingSpotId(),
                    req.getPlatformId(),
                    req.getUid(),
                    req.getPwd(),
                    req.getLongi(),
                    req.getLati(),
                    req.getBatchCode(),
                    req.getName(),
                    req.getAddress(),
                    req.getOpentime(),
                    req.getPrice()
            );
        } else if(request instanceof RequestHeartBeat){
            RequestHeartBeat req = (RequestHeartBeat)request;
            command = new CommandHeartBeat(
                    0,
                    req.getCommRequest().getClientId(),
                    req.getCommRequest().getTsn(),
                    req.getCommRequest().getSim(),
                    req.getCommRequest().getPsam(),
                    req.getCommRequest().getSysVer(),
                    req.getCommRequest().getAppVer(),
                    req.getParkingSpotId(),
                    req.getPlatformId(),
                    req.getWorkStat(),
                    req.getAlarmStat(),
                    req.getIpAddress(),
                    req.getMacAddress()
            );
        } else {

        }
        return command;
    }

    public void saveCommand(CommandBase command) {
        if (command == null) {
            return;
        }

        if(command instanceof CommandSyncParkingInfo) {
            commandSyncParkingInfoRepository.save((CommandSyncParkingInfo) command);
        } else if(command instanceof CommandParkingIn) {
            commandParkingInRepository.save((CommandParkingIn) command);
        } else if(command instanceof CommandParkingOut) {
            commandParkingOutRepository.save((CommandParkingOut) command);
        } else if(command instanceof CommandParkingCheckin) {
            commandParkingCheckinRepository.save((CommandParkingCheckin) command);
        } else if(command instanceof CommandHeartBeat) {
            commandHeartBeatRepository.save((CommandHeartBeat) command);
        } else {

        }
    }
}
